package com.system.user.controller;

import com.system.common.utils.R;
import com.system.user.entity.GasEntity;
import com.system.user.service.GasService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "漏气检查设备表")
@RestController
@RequestMapping("/gas")
public class GasController {

    @Autowired
    private GasService gasService;

    @ApiOperation(value = "查询所有数据")
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public R findAll() {
        List<GasEntity> gas = gasService.findAll();
        return R.ok().put("gas", gas);
    }

    @ApiOperation(value = "模糊查找数据")
    @RequestMapping(value = "/findByValue", method = RequestMethod.POST)
    public R findByValue(@RequestBody GasEntity gasEntity) {
        List<GasEntity> gas = gasService.findBySiteOrNumber(gasEntity);
        return R.ok().put("gas", gas);
    }

    @ApiOperation(value = "添加数据")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public R insert(@RequestBody GasEntity gas) {
        boolean is_success = false;
        try {
            is_success = gasService.add(gas);
            if (is_success) {
                return R.ok("添加成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("添加失败！");
        }
        return R.error("添加失败！");
    }

    @ApiOperation(value = "修改数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public R update(@RequestBody GasEntity gas) {
        boolean is_success = gasService.update(gas);
        if (is_success) {
            return R.ok("添加成功！");
        }
        return R.error("添加失败！");
    }

    @ApiOperation(value = "删除数据")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public R delete(@RequestParam("id") Integer id) {
        boolean is_success = gasService.delete(id);
        if (is_success) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }

}
